function loadFiles(path){
    path = path.replaceAll("\\","/");
    let data = {"filePath":path}
    $.ajax({
        type : "POST",
        dataType: "json",
        contentType : "application/x-www-form-urlencoded",
        data : data,
        async: false ,
        url : "/loadFiles",
        success : function(data) {
            // console.log(data);
            if (data != null){
                setCurrentPath(path);
                loadDatas(data);
            }
        },
        error : function(e) {
            console.log(e);
        }
    });
}

function loadDatas(data){
    $("table").empty();
    for(var i=0;i< data.length;i++){
        let filePath = data[i].filePath.replaceAll("\\","/");
        let $td1 = $("<td></td>").text(data[i].fileName);
        let $td2 = $("<td></td>").text(data[i].lastModifiedDate);
        let $td3 = $("<td></td>");
        let $td4 = $("<td></td>");
        if(data[i].directory){
            $td1 = $("<td onclick='loadFiles(\""+filePath+"\")'></td>").text(data[i].fileName);
        }else{
            $td3.text(data[i].fileSize);
            let $download = $("<button onclick='download(\""+filePath+"\")'>下载</button>")
            $td4.append($download);
        }
        let $tr = $("<tr></tr>").append($td1).append($td2).append($td3).append($td4);
        $("table").append($tr);
    }
}

function setCurrentPath(currentPath){
    currentPath = currentPath.replaceAll("\\","/");
    let paths = currentPath.split("/");
    $("#current-path").empty();
    for(let i = 0; i< paths.length; i++){
        if(paths[i]){
            let $span = $("<span class='path' onclick='loadCurrentFiles(\""+paths[i]+"\")'></span>").text(paths[i]);
            $("#current-path").append($span);
            if(paths[i] != "/"){
                let $break = $("<span>/</span>");
                $("#current-path").append($break);
            }
        } else if(i == 0){
            let $span = $("<span class='path' onclick='loadRootFiles()'></span>").text("/");
            $("#current-path").append($span);
        }
    }

}

function loadParentFiles(){
    let currentPath = $("#current-path").text();
    currentPath = currentPath.replaceAll("\\","/");
    if(currentPath.split("/").length < 2){
        return ;
    }else{
        let parentPath = currentPath.substring(0,currentPath.lastIndexOf("/"));
        if(parentPath.lastIndexOf("/") < 0){
            parentPath = currentPath;
        }else{
            parentPath = parentPath.substring(0,parentPath.lastIndexOf("/")+1);
        }
        loadFiles(parentPath);
    }
}

function loadRootFiles(){
    let currentPath = $("#current-path").text();
    currentPath = currentPath.replaceAll("\\","/");
    if(currentPath.split("/").length < 2){
        return ;
    }else{
        let rootPath = currentPath.substring(0,currentPath.indexOf("/")+1);
        loadFiles(rootPath);
    }
}

function loadCurrentFiles(path){
    let currentPath = $("#current-path").text();
    currentPath = currentPath.replaceAll("\\","/");
    let parentPath = currentPath.substring(0,currentPath.lastIndexOf(path));
    parentPath = parentPath + path + "/";
    loadFiles(parentPath);
}

function download(url){
    url = url.replaceAll("\\","/");
    window.open("/download?url="+url);
}